package com.dkts.oil.util;

import org.locationtech.jts.geom.*;
import java.math.BigDecimal;

public class GeometryPrecisionUtil {

    /**
     * 将 Geometry 中所有坐标保留 6 位小数
     */
    public static Geometry roundCoordinates(Geometry geometry, int scale) {
        geometry.apply(new CoordinateSequenceFilter() {
            @Override
            public void filter(CoordinateSequence seq, int i) {
                double x = round(seq.getX(i), scale);
                double y = round(seq.getY(i), scale);
                seq.setOrdinate(i, 0, x);
                seq.setOrdinate(i, 1, y);
            }

            @Override
            public boolean isDone() {
                return false;
            }

            @Override
            public boolean isGeometryChanged() {
                return true;
            }

            private double round(double value, int scale) {
                return new BigDecimal(value).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
            }
        });
        return geometry;
    }
}

